package com.resou.reader.signin.bindingtask;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.R;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.data.signin.model.BindingTask;

import java.util.List;

import butterknife.BindView;

public class BindingTaskFragment extends ResouBaseFragment<BindingTaskPresenter> implements BindingTaskView {
    private static final String TAG = "BindingTaskFragment";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    public static BindingTaskFragment newInstance() {
        Bundle args = new Bundle();
        BindingTaskFragment fragment = new BindingTaskFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new BindingTaskPresenter(this);
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    protected void retryRefreshData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.recycler_view;
    }

    @Override
    public void initData() {
        mPresenter.loadBingTask();
    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void setTasks(List<BindingTask> tasks) {
        Log.d(TAG, "setTasks: " + tasks.size());
        recyclerView.setAdapter(new BingingTaskAdapter(R.layout.recycler_item_sign_in_binding_task, tasks));
    }

    public class BingingTaskAdapter extends BaseQuickAdapter<BindingTask, BaseViewHolder> {
        public BingingTaskAdapter(int layoutResId, List<BindingTask> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BindingTask item) {
            helper.setText(R.id.tv_task_info, item.getTaskName());

            if (Integer.valueOf(item.getTaskStatus()) == 0) {
                helper.setText(R.id.tv_sign_status, "去完成");
                helper.setTextColor(R.id.tv_sign_status, Color.RED);
                helper.setBackgroundRes(R.id.tv_sign_status, R.drawable.bg_sign_in_red_hollow_button);
            }
            if (Integer.valueOf(item.getTaskStatus()) == 1) {
                helper.setText(R.id.tv_sign_status, "领取");
                helper.setTextColor(R.id.tv_sign_status, Color.WHITE);
                helper.setBackgroundRes(R.id.tv_sign_status, R.drawable.bg_sign_in_red_solid_button);
            }
            if (Integer.valueOf(item.getTaskStatus()) == 2) {
                helper.setText(R.id.tv_sign_status, "已完成");
                helper.setTextColor(R.id.tv_sign_status, getResources().getColor(R.color.CCCCCC));
                helper.setBackgroundRes(R.id.tv_sign_status, R.drawable.bg_sign_in_light_hollow_button);
            }
        }
    }

}
